package com.cnbot.dchttpsdk.view;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;


/**
 * Copyright (c) 2016-a  Hunan Cnbot Co., Ltd. All Rights Reserved.
 *
 * @descriptoin progressView
 * @FileName: ProgressViewHandler.java
 * @author: dc
 * @date: 2019/1/19 14:13
 * @version: 1.0
 */

public class ProgressViewHandler extends Handler {

    public static final int SHOW_PROGRESS_VIEW = 1;
    public static final int DISMISS_PROGRESS_VIEW = 2;
    public static final int DISMISS_PROGRESS_VIEW_DELAY = 3;
    private Context mContext;

    private IProgressView mIProgressView;

    public ProgressViewHandler(Context context,  IProgressView progressView) {
        mContext = context;
        mIProgressView = progressView;

    }

    public ProgressViewHandler(Looper looper, Context context,  IProgressView progressView) {
        super(looper);
        mContext = context;
        mIProgressView = progressView;
    }

    public ProgressViewHandler(Looper looper, Context context) {
        super(looper);
        mContext = context;
    }


    public void setProgressView(  IProgressView progressView){
        mIProgressView = progressView;
    }

    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case SHOW_PROGRESS_VIEW:
                showProgressDialog();
                break;
            case DISMISS_PROGRESS_VIEW:
                dismissProgressDialog();
                break;
            case DISMISS_PROGRESS_VIEW_DELAY:
                dismissProgressDialog();
                break;
            default:break;
        }
    }

    private void showProgressDialog() {
        if (mIProgressView == null) {
            throw new NullPointerException("you should init a progress view which implements IProgressView");
        }
        if (!mIProgressView.isShowing()) {
            //确保Activity没有销毁
            if (!((Activity) mContext).isFinishing()) {
                mIProgressView.showProgressView();
            }
        }
    }

    private void dismissProgressDialog() {
        if (mIProgressView != null) {
            mIProgressView.hideProgressView();
            mIProgressView = null;
        }
    }

}
